﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.BidSystem.Model.View;
using System.Data;

public partial class Company_BargainPrice2 : System.Web.UI.Page, IAccess
{
    string action = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        CurrentAccount.CheckSessionTimeOut(this);
        action = Request.QueryString["action"] == null ? "bargain" : Request.QueryString["action"].ToLower();
        
         int b = BLL_Factory.IBLL_Common.GetBargain(int.Parse(CurrentAccount.Account.AccountId.ToString()));
         //HZ.Web.WebUtil.Alert(b.ToString(), this);
        if (b == 1)
        {
            // 验证报价密码
            //if (!CurrentAccount.IsCheckBidPwd)
            //{
            //    Response.Redirect(string.Format("CheckBidPwd.aspx?action={0}", action));
            //}

            if (!CurrentProject.IsBargainSwitch)
            {
                HZ.Web.MessageBox.Show("议价开关未开启，暂时无法报价。");
            }

            if (CurrentProject.InBargainTime2 < 0)
            {
                HZ.Web.MessageBox.Show("第二轮议价时间未到，请稍后！");
            }

            if (CurrentProject.InBargainTime2 > 0)
            {
                HZ.Web.MessageBox.Show("第二轮议价时间已过！");
            }
        }
        else
        {
            HZ.Web.MessageBox.Show("本次招标，您无议价品种！");
        }
        if (!IsPostBack)
        {
            bidinfo.Text = BLL_Factory.IBLL_Manage.GetConfigurationByKey("BargainDes").Value;
            BindData();
        }
        WebTimer1.EndTime = CurrentProject.ProjectConfig.SecondBargainEndTime.Value;
        WebTimer1.BeginTime = CurrentProject.ProjectConfig.SecondBargainStartTime.Value;
       
    }

    /// <summary>
    /// 确认
    /// </summary> 
    protected void LinkButtonGet_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        if (btn.CommandName == "Yes")
        {  
            string bidId = btn.CommandArgument.Split(',')[0];
            string CompanyID_TB = btn.CommandArgument.Split(',')[1];
            View_BargainPrice2 goods = new View_BargainPrice2(); //记录日志用
            goods.BidId = bidId;
            goods.CompanyId_Tb =Convert.ToInt32(CompanyID_TB);
            goods.Switch =1; 
            int CheckSecces = BLL_Factory.IBLL_Company.UpdateBarinPrice2(goods);
            if (CheckSecces == 1)
            {
                HZ.Common.log4netManager.LogInfo("企业确认第二轮价格", string.Format("企业确认第二轮价格：企业编号：{0}，报价IP：{1}，报价产品：{2}", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, bidId), null);
                HZ.Web.WebUtil.Alert("操作成功！", this);
                BindData();
            }
            else
            {
                HZ.Web.WebUtil.Alert("操作失败，请检查数据！", this);
            }
        }
    }

    /// <summary>
    /// 弃标
    /// </summary> 
    protected void LinkButtonUNGet_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        if (btn.CommandName == "No")
        {  
            string bidId = btn.CommandArgument.Split(',')[0];
            string CompanyID_TB = btn.CommandArgument.Split(',')[1];
            View_BargainPrice2 goods = new View_BargainPrice2(); //记录日志用
            goods.BidId = bidId;
            goods.CompanyId_Tb = Convert.ToInt32(CompanyID_TB);
            goods.Switch = 2; 
            int CheckSecces = BLL_Factory.IBLL_Company.UpdateBarinPrice2(goods);
            if (CheckSecces == 1)
            {
                HZ.Common.log4netManager.LogInfo("企业弃标", string.Format("企业弃标：企业编号：{0}，报价IP：{1}，报价产品：{2}", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, bidId), null);
                HZ.Web.WebUtil.Alert("操作成功！", this);
                BindData();
            }
            else
            {
                HZ.Web.WebUtil.Alert("操作失败，请检查数据！", this);
            }
        }
    }
     
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        pager1.CurrentPage = 1;
        BindData();
    }
    protected void BindData()
    {
        int totalrecord;
        string append = "1=1";
        if (ddlSwitch.SelectedValue != "全部")
        {
            append = string.Format(@"{0}", ddlSwitch.SelectedValue);
        }
        DataTable dt = BLL_Factory.IBLL_Company.GetBarinBidPrice2(new HZ.BidSystem.Model.QBidPrice() { CompanyId_Tb = CurrentAccount.Account.AccountId, SortFields = "Company_Goods", ProductName = txtProdcutName.Text, ModelSmall = txtModel.Text, Outlookc = txtOutlookc.Text, Append = append }, pager1.CurrentPage, pager1.PageSize, out totalrecord, CurrentAccount.BidPwd, CurrentAccount.RandPwd);
        pager1.RecordCount = totalrecord;
        gvbidprice.DataSource = dt;
        gvbidprice.DataBind();

    }

    protected void pager1_PageChanging(object sender, HZ.Web.PageChaningEventArgs e)
    {
        BindData();
    }
    #region IAccess Members

    IList<EnumAccess> IAccess.AccessList
    {
        get
        {
            var list = new List<EnumAccess>();
            list.Add(EnumAccess.Company);
            return list;
        }

    }

    #endregion
    protected void gvbidprice_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblCompany_goods = e.Row.FindControl("lblCompany_goods") as Label;
            Label lblLimitPrice = e.Row.FindControl("lblLimitPrice") as Label;
            DataTable dt = BLL_Factory.IBLL_Company.getNoBarinPrice(lblCompany_goods.Text);
            if (dt.Rows.Count > 0)
            {
                lblLimitPrice.Visible = false;
            }
        }
    }

    /// <summary>
    /// 获取状态
    /// </summary>
    /// <param name="o"></param>
    /// <returns></returns>
    public string GetBarState(object o)
    {
        string id = o.ToString(), value = "";

        switch (id)
        {
            case null:
                value = "未处理";
                break;
            case "":
                value = "未处理";
                break;
            case "0":
                value = "未处理";
                break;
            case "1":
                value = "已确认";
                break;
            case "2":
                value = "已弃标"; 
                break;
        }
        return value;
    }
    protected void btnSelect_Click(object sender, EventArgs e)
    {
        BindData();
    }
}
